﻿// -----------------------------------------------------------------------
// <copyright file="TrainsViewModel.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TrainsAndTimeTables.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
using TrainsAndTimeTables.Models;
using System.Collections.ObjectModel;
using RailroadDal;
using System.Windows.Input;
    using TrainsAndTimeTables.Commands;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class TrainsViewModel
    {
        private ModelRautes model;
        ICommand updateRautes;
        ICommand updateTable;
        private List<Raute> stationRautes;
        private List<string> rauteAsString;
        private List<TimeTableEntery> timeTable;

        public List<TimeTableEntery> TimeTable
        {
            get
            {
                return model.TimeTable;
            }
            
        }

        public List<string> RauteAsString
        {
            get
            {
                List<string> strings = GenerateStringPath();
                return strings;
            }
            
        }

        public List<Raute> StationRautes
        {
            get
            {
                return stationRautes;
            }
            set
            {
                stationRautes = value;
            }
        }

        public ICommand UpdateRautes
        {
            get
            {
                if (updateRautes == null)
                {
                    this.updateRautes = new ParameterRelayCommand((object obj) =>
                    {
                        var rautes = this.Stations.FirstOrDefault(s => s.StationId == (int)obj).Rautes;
                        this.stationRautes = rautes.ToList() ;
                    });
                }
                return updateRautes;
            }
        }

        public ICommand UpdateTable
        {
            get
            {
                if (updateTable == null)
                {
                    this.updateTable = new ParameterRelayCommand((object obj) =>
                    {
                        model.GenerateTimeTable((int)obj);
                    });
                }
                return updateTable;
            }
        }

        public ObservableCollection<Station> Stations
        {
            get
            {
                return this.model.Stations;
            }
        }

        public TrainsViewModel()
        {
            this.model = new ModelRautes();
        }

        private List<string> GenerateStringPath() 
        {
            List<string> rautesAsString = new List<string>();
            StringBuilder builder = new StringBuilder();
            foreach (var raute in this.StationRautes)
            {
                foreach (var station in raute.Stations)
                {
                    builder.Append(station.StationName);
                    builder.Append("-->");
                }
                builder.Remove(builder.Length - 3, 3);
                rautesAsString.Add(builder.ToString());
                builder.Clear();
            }
            return rautesAsString;
        }
    }
}
